package top.lyc829.aircontroller.ui.searchVideo;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import top.lyc829.aircontroller.R;
import top.lyc829.aircontroller.logic.dao.Video;
import top.lyc829.aircontroller.logic.utils.Sout;

public class VideoListAdapter extends ArrayAdapter<Video> {

    private int viewId;
    private SimpleDateFormat dateFormat;

    @SuppressLint("SimpleDateFormat")
    public VideoListAdapter(@NonNull Context context, int resource, @NonNull List<Video> objects) {
        super(context, resource, objects);
        this.viewId = resource;
        dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Video video = getItem(position);

        Sout.i(getContext().getClass().getName());

        View view = LayoutInflater.from(getContext()).inflate(viewId,null);
        ImageView videoPic = view.findViewById(R.id.video_pic);
        TextView nameText = view.findViewById(R.id.name_text);
        TextView authText = view.findViewById(R.id.auth_text);
        TextView dateText = view.findViewById(R.id.date_text);
        TextView playText = view.findViewById(R.id.play_text);

        Glide.with(getContext()).load(video.getImgUrl()).centerCrop().into(videoPic);
        nameText.setText(video.getName());
        nameText.getPaint().setFakeBoldText(true);
        authText.setText(video.getAuth());
        dateText.setText(getDate(video.getDate()));
        if (getContext().getClass().getName().equals("com.cxc.video.activity.VideoCacheActivity")){
            playText.setVisibility(View.GONE);
            view.findViewById(R.id.play_icon).setVisibility(View.GONE);
        }else {
            playText.setText(video.getPlay());
        }

        return view;
    }

    private String getDate(String date){
        long stamp = Long.parseLong(date);
        Date d = new Date(stamp * 1000);
        return dateFormat.format(d);
    }

}
